home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv4n2.zip / CPP.ZIP / LLIST.HXX < prev    next >
Text File  |  1989-08-26  |  2KB  |  51 lines

  1. // LLIST.HXX
  2. // Generic Linked-list container class.
  3. // Inherit this class into a class which specifies the type of
  4. // object to be stored in the list by re-defining the arguments
  5. // and return values (see LLTEST.CXX for an example).
  6. //   Notice that the linked-list maintains the order of the list,
  7. // and allows you to sort the list, etc., while a "bag" container
  8. // like a DynArray ignores order.
  9. //   This class works as a container only for pointers to different
  10. // kinds of objects.  You need templates to be able to contain
  11. // the objects themselves (a feature which will be added in a
  12. // release after 2.0).
  13. #ifndef _LLIST_HXX
  14. #define _LLIST_HXX
  15.  
  16. struct llist_el {
  17.   void * data;  // pointer to the object stored in the list
  18.   llist_el * next;  // This is a singly-linked list.
  19. };
  20.  
  21. class llist {
  22.   llist_el * head;
  23.   llist_el * tail;
  24.   llist_el * cursor;
  25.   llist_el * previous;  // element before cursor (for linking & unlinking)
  26. protected:
  27.   // to properly delete the data element:
  28.   virtual void delete_data(void *) {};
  29.   // When you inherit this class, you must re-define this 
  30.   // function like so:
  31.   // void delete_data(void * dp) {
  32.   //   delete (my_data_type *)dp;
  33.   // }
  34.   // Where my_data_type is the type of object to be contained
  35.   // in the derived class.
  36. public:
  37.   llist();
  38.   void append(void *); // add to end of list
  39.   void insert(void *); // insert before cursor
  40.   void push_to_end(); // move current element to end of list
  41.   // nonzero return value indicates failure:
  42.   int remove(); // delete current link
  43.   void reset();  // reset cursor to beginning of list
  44.   int next();  // nonzero return value means cursor moved
  45.   int end(); // nonzero value means you are at the end of the list
  46.   void * value(); // pointer of the current data value
  47.   ~llist();  // remove all elements from the list
  48. };
  49.  
  50. #endif // _LLIST_HXX
  51.